// Author: wsfuyibing <682805@qq.com>
// Date: 2025-04-27

package config

import (
	"gitee.com/go-libs/config"
	"time"
)

// WeiXin
// 微信配置实例.
var WeiXin WeiXinConfig

// 默认配置.

const (
	defaultWeixinApiHost       = "https://api.weixin.qq.com"
	defaultWeixinContentType   = "application/json"
	defaultWeixinTimeout       = 10
	defaultWeixinWithdrawalMin = 20.0
	defaultWeixinWithdrawalMax = 200.0
)

type (
	// WeiXinConfig
	// 微信配置接口.
	WeiXinConfig interface {
		GetAppHost() string
		GetAppId() string
		GetAppSecret() string
		GetWithdrawalMax() float64
		GetWithdrawalMin() float64
		GetContentType() string
		GetTimeout() int
		GetTimeoutDuration() time.Duration
	}

	weixinConfig struct {
		AppHost   string `yaml:"app-host"`
		AppId     string `yaml:"app-id"`
		AppSecret string `yaml:"app-secret"`

		WithdrawalMin float64 `yaml:"withdrawal-min"`
		WithdrawalMax float64 `yaml:"withdrawal-max"`

		ContentType     string        `yaml:"content-type"`
		Timeout         int           `yaml:"timeout"`
		TimeoutDuration time.Duration `yaml:"-"`
	}
)

// +---------------------------------------------------------------------------+
// | Interface methods                                                         |
// +---------------------------------------------------------------------------+

func (o *weixinConfig) GetAppHost() string   { return o.AppHost }
func (o *weixinConfig) GetAppId() string     { return o.AppId }
func (o *weixinConfig) GetAppSecret() string { return o.AppSecret }

func (o *weixinConfig) GetWithdrawalMax() float64 { return o.WithdrawalMax }
func (o *weixinConfig) GetWithdrawalMin() float64 { return o.WithdrawalMin }

func (o *weixinConfig) GetContentType() string            { return o.ContentType }
func (o *weixinConfig) GetTimeout() int                   { return o.Timeout }
func (o *weixinConfig) GetTimeoutDuration() time.Duration { return o.TimeoutDuration }

// +---------------------------------------------------------------------------+
// | Hook methods                                                              |
// +---------------------------------------------------------------------------+

func (o *weixinConfig) After() {
	if o.AppHost == "" {
		o.AppHost = defaultWeixinApiHost
	}
	if o.WithdrawalMin == 0 {
		o.WithdrawalMin = defaultWeixinWithdrawalMin
	}
	if o.WithdrawalMax == 0 {
		o.WithdrawalMax = defaultWeixinWithdrawalMax
	}
	if o.ContentType == "" {
		o.ContentType = defaultWeixinContentType
	}
	if o.Timeout == 0 {
		o.Timeout = defaultWeixinTimeout
	}
	o.TimeoutDuration = time.Duration(o.Timeout) * time.Second
}

// +---------------------------------------------------------------------------+
// | Access methods                                                            |
// +---------------------------------------------------------------------------+

func (o *weixinConfig) init() *weixinConfig {
	_ = config.Seek("weixin.yaml", "weixin.yml").ScanYaml(o)
	return o
}
